package com.travel.server.manager.impl.command.data;

import com.travel.common.excel.ExcelData;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.data.ExportPageDataRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.data.QueryPageDataResponse.VisitAreaItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 19:11
 * @Desc:
 */
@Component
@Slf4j
public class ExportPageDataCommand extends AbstractCommand<ExportPageDataRequest, EmptyResponse> {

    private final Integer PAGE = 1;

    private final Integer PAGE_TRANSFER = 2;

    private final Integer AREA = 3;

    @Autowired
    private QueryPageDataCommand queryPageDataCommand;

    @Override
    protected void checkInput(ExportPageDataRequest request) {
        if(request.getType() == null || request.getType() < 1 || request.getType() > 3){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(ExportPageDataRequest request) {
        Integer type = request.getType();
        List<String> title;
        List<List<Object>> rows = new ArrayList<>();
        String fileName;
        if(PAGE.equals(type)){
            fileName = "页面转化数据";
            List<Object> objects = new ArrayList<>();
            title = Arrays.asList("访客数", "浏览量", "商品访客数", "商品曝光次数", "商品浏览量");
            objects.add("0");
            objects.add("0");
            objects.add("0");
            objects.add("0");
            objects.add("0");
            rows.add(objects);
        }else if(PAGE_TRANSFER.equals(type)){
            fileName = "单页面转化数据";
            title = Arrays.asList("页面名称", "页面类型", "浏览量", "曝光次数", "访客数");
        }else {
            fileName = "访客地域分布";
            title = Arrays.asList("序号", "地区", "浏览量", "访客数", "商品浏览量", "商品访问数");
            List<VisitAreaItem> list = queryPageDataCommand.makeAreaList();
            for(VisitAreaItem item : list){
                List<Object> objects = new ArrayList<>();
                objects.add(item.getNum());
                objects.add(item.getArea());
                objects.add(item.getBrowser());
                objects.add(item.getVisit());
                objects.add(item.getCommodityBrowser());
                objects.add(item.getCommodityVisit());
                rows.add(objects);
            }
        }
        ExcelData excelData = new ExcelData(title , rows, fileName);
        ExcelUtil.exportExcel(getHttpServletResponse(), fileName, excelData);

        return new EmptyResponse();
    }
}
